package entity.connector;

import java.util.Calendar;

/**
 *
 * @author 52571783
 */
public class TimeConvertor {

    public static Calendar stringToCalendar(String timeString) {
        int year = Integer.parseInt(timeString.substring(1, 4));
        int month = Integer.parseInt(timeString.substring(6, 7));
        int day = Integer.parseInt(timeString.substring(9, 10));
        int hour = Integer.parseInt(timeString.substring(11, 13));
        int minute = Integer.parseInt(timeString.substring(14, 16));
        Calendar c = Calendar.getInstance();
        c.set(year, month, day, hour, minute);
        return c;
    }

    public static String calendarToString(Calendar time) {
        String timeString = "20";
        timeString += time.get(Calendar.YEAR) + "-";
        timeString += time.get(Calendar.MONTH) + "-";
        timeString += time.get(Calendar.DAY_OF_MONTH) + " ";
        timeString += time.get(Calendar.HOUR_OF_DAY) + ":";
        timeString += time.get(Calendar.MINUTE);
        return timeString;
    }

    public static String printTime(Calendar time) {
        String timeString = "";
        switch (time.get(Calendar.DAY_OF_WEEK)) {
            case 1:
                timeString = "Mon";
                break;
            case 2:
                timeString = "Tue";
                break;
            case 3:
                timeString = "Wed";
                break;
            case 4:
                timeString = "Thu";
                break;
            case 5:
                timeString = "Fri";
                break;
            case 6:
                timeString = "Sat";
                break;
            case 7:
                timeString = "Sun";
                break;
        }
        timeString += ", ";
        switch (time.get(Calendar.MONTH)) {
            case 1:
                timeString += "Jan";
                break;
            case 2:
                timeString += "Feb";
                break;
            case 3:
                timeString += "Mar";
                break;
            case 4:
                timeString += "Apr";
                break;
            case 5:
                timeString += "May";
                break;
            case 6:
                timeString += "Jan";
                break;
            case 7:
                timeString += "Jul";
                break;
            case 8:
                timeString += "Aug";
                break;
            case 9:
                timeString += "Sep";
                break;
            case 10:
                timeString += "Oct";
                break;
            case 11:
                timeString += "Nov";
                break;
            case 12:
                timeString += "Dec";
                break;
        }
        timeString += " " + fill(time.get(Calendar.DAY_OF_MONTH));
        timeString += ", " + fill(time.get(Calendar.HOUR));
        timeString += ":" + fill(time.get(Calendar.MINUTE));
        switch (time.get(Calendar.AM_PM)) {
            case Calendar.AM:
                timeString += "AM";
                break;
            case Calendar.PM:
                timeString += "PM";
                break;
        }
        return timeString;
    }

    public static String fill(int time) {
        if (time < 10) {
            return "0" + time;
        }
        return "" + time;
    }
}
